import { defineStore } from "pinia";
import { ref } from "vue";
import { login, loginout } from "@/api/login";
import { setToken, getToken, delToken } from "@/utils/token";
import { setStorage, delStorage } from "@/utils/storage";

export const useLoginStore = defineStore("Login", () => {
  let codeTime = ref(60);
  let codeTimeInterval = null;
  const loginFn = async (param) => {
    const res = await login(param);

    setToken(res.data.token);
    // 没有二级菜单 左侧缩进则会异常
    const menu = [
      { path: "", meta: { title: "首页" }, children: [] },
      { path: "", meta: { title: "账号管理" }, children: [] },
      { path: "", meta: { title: "商品管理" }, children: [] },
      { path: "", meta: { title: "订单管理" }, children: [
          { path: "", meta: { title: "全部订单" } },
          { path: "", meta: { title: "待付款" } },
          { path: "", meta: { title: "待发货" } },
          { path: "", meta: { title: "待收货" } },
          { path: "", meta: { title: "订单完成" } }
      ]},

    ];
    setStorage("userInfo", {
      shop_id:res.data.shop_id,
      menu:menu,
    });
    return res;
  };
  const loginoutFn = async () => {
    // const res = await loginout();
    delToken();
    delStorage("userInfo");
    // return res;
  };
  const startCodeTime = () => {
    if (codeTimeInterval) return;
    codeTimeInterval = setInterval(() => {
      codeTime.value--;
      if (codeTime.value <= 0) {
        clearInterval(codeTimeInterval);
        codeTime.value = 60;
      }
    }, 1000);
  };
  const closeCodeTime = () => {
    clearInterval(codeTimeInterval);
    codeTime = 60;
  };
  return { loginoutFn: loginoutFn, loginFn, startCodeTime, closeCodeTime, codeTime };
});
